﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using Hangjing.DBUtility;
using Hangjing.Model;

namespace Hangjing.SQLServerDAL
{
    /// <summary>
    /// 入出库订单
    /// </summary>
    public partial class StorageOrder
    {

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(StorageOrderInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into StorageOrder(");
            strSql.Append("adminname,addtime,reveint1,sremark,orderid,reveint2,stype,reveint3,revevar1,revevar2,revevar3,revedate1,revedate2,cityid,gid");
            strSql.Append(") values (");
            strSql.Append("@adminname,@addtime,@reveint1,@sremark,@orderid,@reveint2,@stype,@reveint3,@revevar1,@revevar2,@revevar3,@revedate1,@revedate2,@cityid,@gid");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters =
            {
                        new SqlParameter("@adminname", SqlDbType.VarChar,256) ,
                        new SqlParameter("@addtime", SqlDbType.DateTime) ,
                        new SqlParameter("@reveint1", SqlDbType.Int,4) ,
                        new SqlParameter("@sremark", SqlDbType.VarChar,256) ,
                        new SqlParameter("@orderid", SqlDbType.VarChar,50) ,
                        new SqlParameter("@reveint2", SqlDbType.Int,4) ,
                        new SqlParameter("@stype", SqlDbType.Int,4) ,
                        new SqlParameter("@reveint3", SqlDbType.Int,4) ,
                        new SqlParameter("@revevar1", SqlDbType.VarChar,256) ,
                        new SqlParameter("@revevar2", SqlDbType.VarChar,256) ,
                        new SqlParameter("@revevar3", SqlDbType.VarChar,256) ,
                        new SqlParameter("@revedate1", SqlDbType.DateTime) ,
                        new SqlParameter("@revedate2", SqlDbType.Float,8) ,
                        new SqlParameter("@cityid", SqlDbType.Int,4) ,
                        new SqlParameter("@gid", SqlDbType.Int,4)

            };

            parameters[0].Value = model.adminname;
            parameters[1].Value = model.addtime;
            parameters[2].Value = model.reveint1;
            parameters[3].Value = model.sremark;
            parameters[4].Value = model.orderid;
            parameters[5].Value = model.reveint2;
            parameters[6].Value = model.stype;
            parameters[7].Value = model.reveint3;
            parameters[8].Value = model.revevar1;
            parameters[9].Value = model.revevar2;
            parameters[10].Value = model.revevar3;
            parameters[11].Value = model.revedate1;
            parameters[12].Value = model.revedate2;
            parameters[13].Value = model.cityid;
            parameters[14].Value = model.gid;
            return HJConvert.ToInt32(SQLHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters));
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(StorageOrderInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update StorageOrder set ");

            strSql.Append(" adminname = @adminname , ");
            strSql.Append(" addtime = @addtime , ");
            strSql.Append(" reveint1 = @reveint1 , ");
            strSql.Append(" sremark = @sremark , ");
            strSql.Append(" orderid = @orderid , ");
            strSql.Append(" reveint2 = @reveint2 , ");
            strSql.Append(" stype = @stype , ");
            strSql.Append(" reveint3 = @reveint3 , ");
            strSql.Append(" revevar1 = @revevar1 , ");
            strSql.Append(" revevar2 = @revevar2 , ");
            strSql.Append(" revevar3 = @revevar3 , ");
            strSql.Append(" revedate1 = @revedate1 , ");
            strSql.Append(" revedate2 = @revedate2 , ");
            strSql.Append(" cityid = @cityid , ");
            strSql.Append(" gid = @gid  ");
            strSql.Append(" where Id=@Id ");

            SqlParameter[] parameters =
            {
                        new SqlParameter("@Id", SqlDbType.Int,4) ,
                        new SqlParameter("@adminname", SqlDbType.VarChar,256) ,
                        new SqlParameter("@addtime", SqlDbType.DateTime) ,
                        new SqlParameter("@reveint1", SqlDbType.Int,4) ,
                        new SqlParameter("@sremark", SqlDbType.VarChar,256) ,
                        new SqlParameter("@orderid", SqlDbType.VarChar,50) ,
                        new SqlParameter("@reveint2", SqlDbType.Int,4) ,
                        new SqlParameter("@stype", SqlDbType.Int,4) ,
                        new SqlParameter("@reveint3", SqlDbType.Int,4) ,
                        new SqlParameter("@revevar1", SqlDbType.VarChar,256) ,
                        new SqlParameter("@revevar2", SqlDbType.VarChar,256) ,
                        new SqlParameter("@revevar3", SqlDbType.VarChar,256) ,
                        new SqlParameter("@revedate1", SqlDbType.DateTime) ,
                        new SqlParameter("@revedate2", SqlDbType.Float,8) ,
                        new SqlParameter("@cityid", SqlDbType.Int,4) ,
                        new SqlParameter("@gid", SqlDbType.Int,4)

            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.adminname;
            parameters[2].Value = model.addtime;
            parameters[3].Value = model.reveint1;
            parameters[4].Value = model.sremark;
            parameters[5].Value = model.orderid;
            parameters[6].Value = model.reveint2;
            parameters[7].Value = model.stype;
            parameters[8].Value = model.reveint3;
            parameters[9].Value = model.revevar1;
            parameters[10].Value = model.revevar2;
            parameters[11].Value = model.revevar3;
            parameters[12].Value = model.revedate1;
            parameters[13].Value = model.revedate2;
            parameters[14].Value = model.cityid;
            parameters[15].Value = model.gid;
            return SQLHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);
        }


        /// <summary>
        /// 获取记录所有字段
        /// </summary>
        /// 此代码由杭景科技代码内部生成器自动生成
        /// <param>Id</param>
        /// <returns>StorageOrderInfo</returns>
        public StorageOrderInfo GetModel(int Id)
        {
            string sql = "select Id,adminname,addtime,reveint1,sremark,orderid,reveint2,stype,reveint3,revevar1,revevar2,revevar3,revedate1,revedate2,cityid,gid from StorageOrder where  Id = @Id";
            SqlParameter parameter = new SqlParameter("@Id", SqlDbType.Int, 4);
            parameter.Value = Id;
            StorageOrderInfo model = null;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, sql, parameter))
            {
                if (dr.Read())
                {
                    model = new StorageOrderInfo();
                    model.Id = HJConvert.ToInt32(dr["Id"]);
                    model.adminname = HJConvert.ToString(dr["adminname"]);
                    model.addtime = HJConvert.ToDateTime(dr["addtime"]);
                    model.reveint1 = HJConvert.ToInt32(dr["reveint1"]);
                    model.sremark = HJConvert.ToString(dr["sremark"]);
                    model.orderid = HJConvert.ToString(dr["orderid"]);
                    model.reveint2 = HJConvert.ToInt32(dr["reveint2"]);
                    model.stype = HJConvert.ToInt32(dr["stype"]);
                    model.reveint3 = HJConvert.ToInt32(dr["reveint3"]);
                    model.revevar1 = HJConvert.ToString(dr["revevar1"]);
                    model.revevar2 = HJConvert.ToString(dr["revevar2"]);
                    model.revevar3 = HJConvert.ToString(dr["revevar3"]);
                    model.revedate1 = HJConvert.ToDateTime(dr["revedate1"]);
                    model.revedate2 = HJConvert.ToDecimal(dr["revedate2"]);
                    model.cityid = HJConvert.ToInt32(dr["cityid"]);
                    model.gid = HJConvert.ToInt32(dr["gid"]);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得总记录数
        /// </summary>
        /// 此代码由杭景科技代码内部生成器自动生成
        public int GetCount(string strWhere)
        {
            return (int)SQLHelper.ExecuteScalar(CommandType.StoredProcedure, "pagecount", new SqlParameter("@tblName", "StorageOrder"), new SqlParameter("@strWhere", strWhere));
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="pagesize">页尺寸</param>
        /// <param name="pageindex">页索引</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="orderName">排序字段</param>
        /// <param name="orderType">排序类型，1为降序，0为升序</param>
        /// <returns>列表</returns>
        ///此代码由杭景科技代码内部生成器自动生成
        public IList<StorageOrderInfo> GetList(int pagesize, int pageindex, string strWhere, string orderName, int orderType)
        {
            IList<CityInfo> citys = CacheHelper.GetCityList();
            IList<DeliverGroupInfo> DeliverGroup = CacheHelper.GetEdelivergroupList();

            IList<StorageOrderInfo> infos = new List<StorageOrderInfo>();
            SqlParameter[] parameters =
                    {
                new SqlParameter("@tblName", SqlDbType.VarChar,255),
                new SqlParameter("@strGetFields", SqlDbType.VarChar,1000),
                new SqlParameter("@primary", SqlDbType.VarChar,255),
                new SqlParameter("@orderName", SqlDbType.VarChar,255),
                new SqlParameter("@PageSize", SqlDbType.Int),
                new SqlParameter("@PageIndex", SqlDbType.Int),
                new SqlParameter("@OrderType", SqlDbType.Bit),
                new SqlParameter("@strWhere", SqlDbType.VarChar,1500)
            };
            parameters[0].Value = "StorageOrder";
            parameters[1].Value = "Id,adminname,addtime,reveint1,sremark,orderid,reveint2,stype,reveint3,revevar1,revevar2,revevar3,revedate1,revedate2,cityid,gid";
            parameters[2].Value = "Id";
            parameters[3].Value = orderName;
            parameters[4].Value = pagesize;
            parameters[5].Value = pageindex;
            parameters[6].Value = orderType;
            parameters[7].Value = strWhere;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "pageselectpri", parameters))
            {
                while (dr.Read())
                {
                    StorageOrderInfo model = new StorageOrderInfo();
                    model.Id = HJConvert.ToInt32(dr["Id"]);
                    model.adminname = HJConvert.ToString(dr["adminname"]);
                    model.addtime = HJConvert.ToDateTime(dr["addtime"]);
                    model.reveint1 = HJConvert.ToInt32(dr["reveint1"]);
                    model.sremark = HJConvert.ToString(dr["sremark"]);
                    model.orderid = HJConvert.ToString(dr["orderid"]);
                    model.reveint2 = HJConvert.ToInt32(dr["reveint2"]);
                    model.stype = HJConvert.ToInt32(dr["stype"]);
                    model.reveint3 = HJConvert.ToInt32(dr["reveint3"]);
                    model.revevar1 = HJConvert.ToString(dr["revevar1"]);
                    model.revevar2 = HJConvert.ToString(dr["revevar2"]);
                    model.revevar3 = HJConvert.ToString(dr["revevar3"]);
                    model.revedate1 = HJConvert.ToDateTime(dr["revedate1"]);
                    model.revedate2 = HJConvert.ToDecimal(dr["revedate2"]);
                    model.cityid = HJConvert.ToInt32(dr["cityid"]);
                    model.cityname = "";
                    foreach (var item in citys)
                    {
                        if (item.cid == model.cityid)
                        {
                            model.cityname = item.cname;
                            break;
                        }
                      
                    }

                    model.gid = HJConvert.ToInt32(dr["gid"]);
                    model.gname = "";
                    foreach (var item in DeliverGroup)
                    {
                        if (item.ID == model.gid)
                        {
                            model.gname = item.classname;
                            break;
                        }
                       
                    }


                    infos.Add(model);
                }
            }
            return infos;
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="Id"></param>
        /// 此代码由杭景科技代码内部生成器自动生成
        /// <returns></returns>
        public int DelStorageOrder(int Id)
        {
            StringBuilder str = new StringBuilder();
            str.Append("delete from StorageOrder where Id=@Id");
            SqlParameter[] Para =
                {
                new SqlParameter("@Id",SqlDbType.Int)
            };
            Para[0].Value = Id;

            return SQLHelper.ExecuteNonQuery(CommandType.Text, str.ToString(), Para);
        }


        /// <summary>
        /// 批量删除记录
        /// </summary>
        /// <param name="IdList"></param>
        /// <returns></returns>
        /// 此代码由杭景科技代码内部生成器自动生成
        public int DelList(string IdList)
        {
            StringBuilder str = new StringBuilder();
            str.Append("delete from StorageOrder where Id in ({0})");

            return SQLHelper.ExecuteNonQuery(CommandType.Text, string.Format(str.ToString(), IdList), null);
        }


    }
}

